//package com.javachmapi.springbootjsoup.croner;
//
//import com.llk.utils.AreaHelper;
//import lombok.extern.slf4j.Slf4j;
//import net.javacrumbs.shedlock.core.SchedulerLock;
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;
//import org.apache.http.HttpEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//import uw.dao.DaoFactory;
//import uw.dao.DataSet;
//import zwy.saasdataanalysis.constant.ApiConfig;
//import zwy.saasdataanalysis.service.PeptileTaskLog;
//import zwy.saasdataanalysis.tool.HttpUtil;
//
//import java.time.LocalDate;
//import java.time.format.DateTimeFormatter;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author songlijun
// * @desc: 采集旅划算产品
// * @Date: 2019\10\9 0009
// */
//@Slf4j
//@Service
//public class PeptileLhsTask {
//
//    @Autowired
//    private JdbcTemplate jdbcTemplate;
//
//
//    @Scheduled(cron = "0 0 1 * * ?")
//    @SchedulerLock(name = "lhuasuanqianggoJob", lockAtLeastFor = 1000 * 30)
//    public void lhuasuanqianggoJob()  {
//        int sum = 0;
//        String state = "0";
//        //获取当前系统日期
//        LocalDate date = LocalDate.now();
//        String sysdate = date.format(DateTimeFormatter.BASIC_ISO_DATE);
//
//
//
//
//
//        CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例
//        try{
//            logger.info("#####################开始定时采集旅划算抢购#####################");
//            //根据字典查询出所有城市
//            DataSet areas = dao.queryForDataSet(ApiConfig.SAAS_DATA,"select lhs_area_id,lhs_area_name from reptile_product_area_lhs");
//            while (areas.next()){
//                try {
//                    HttpPost httpost = new HttpPost("https://fx3-guangdong.lhs11.com/fxapi/services/system/ctmall/homepage@storeShelfInfo.japi?variables%5Bdomain%5D=fx3-guangdong.lhs11.com&variables%5Bshelf_id%5D=487&variables%5BcityName%5D=%E5%B9%BF%E5%B7%9E&variables%5Bcity_id%5D="+areas.getString("lhs_area_id")); // 创建httpget实例
//                    httpost.setHeader("cookie", "usertoken=25i02up0UFvnUkv12uxIT57X; traefik-sticky=http://10.42.144.37:8080; SERVERID=5f4b74c0eaf529789974af9716490738|1579586118|1579585351");
//                    CloseableHttpResponse response = httpClient.execute(httpost);
//                    HttpEntity entity = response.getEntity(); // 获取返回实体
//                    String jsonStr = EntityUtils.toString(entity, "utf-8");
//                    //初始化对象
//                    JSONObject jsonObject = JSONObject.fromObject(jsonStr);
//                    //得到第一个data对象
//                    JSONObject result = null;
//                    try {
//                        result = jsonObject.getJSONObject("result");
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    if(result!=null){
//
//                        JSONObject shelfInfo = null;
//                        try {
//                            shelfInfo = result.getJSONObject("shelfInfo");
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        JSONArray productInfo = null;
//                        try {
//                            productInfo = shelfInfo.getJSONArray("productInfo");
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        String biz="旅划算";
//                        String area_id = AreaHelper.getAreaIDForName(areas.getString("lhs_area_name"));
//                        String area_name = areas.getString("lhs_area_name");
//                        String out_area_id=areas.getString("lhs_area_id");
//                        String out_area_name=areas.getString("lhs_area_name");
//                        String tree_name=shelfInfo.getString("name");
//                        String market_price="0";
//                        String storage=null;
//                        String context=null;
//                        for (int j = 0; j <= productInfo.size()-1; j++) {
//                            try {
//                                String info_id = null;
//                                try {
//                                    info_id = productInfo.getJSONObject(j).optString("id");
////                                    if(dao.queryForDataSet(ApiConfig.SAAS_DATA,"select info_id from reptile_product where biz='zby' and info_id="+info_id).size()>0){
////                                        dao.executeCommand(ApiConfig.SAAS_DATA,"insert into reptile_product_lhs_"+sysdate+" (biz,info_id)values(?,?)",
////                                                new Object[]{biz,info_id});
////                                        continue;
////                                    }
//                                } catch (Exception e) {
//
//                                }
//                                String info_title = null;
//                                try {
//                                    info_title = productInfo.getJSONObject(j).optString("name");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String img = null;
//                                try {
//                                    img = productInfo.getJSONObject(j).optString("img_main");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String order_start_date = null;
//                                try {
//                                    order_start_date = productInfo.getJSONObject(j).optString("begin_time");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//
//                                String order_end_date = null;
//                                try {
//                                    order_end_date = productInfo.getJSONObject(j).optString("end_time");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String price = "0";
//                                try {
//                                    price = String.valueOf(new Double((Double.valueOf(productInfo.getJSONObject(j).getString("price"))*100)).intValue());
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String order_num = null;
//                                try {
//                                    order_num = productInfo.getJSONObject(j).optString("saled_num");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String link="https://cdn-fx3-api.lhs11.com/fxc/services/ms/fxc/product@getProductDetail.japi?variables%5Bproduct_id%5D="+info_id+"&variables%5Bclient_width%5D=450";
//                                //下面请求详情获取详情中的内容
//                                JSONObject xiangqing = null;
//                                try {
//                                    xiangqing = JSONObject.fromObject(HttpUtil.doGet(link,""));
//                                } catch (Exception e) {
//                                    PeptileTaskLog.pullproductLog("旅划算抢购","采集失败"+e.getMessage(),0,state);
//                                }
//                                JSONObject xiangqing_result = null;
//                                try {
//                                    xiangqing_result = xiangqing.getJSONObject("result");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                JSONObject xiangqing_productInfo = null;
//                                try {
//                                    xiangqing_productInfo = xiangqing_result.getJSONObject("productInfo");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String address = null;
//                                try {
//                                    address = xiangqing_productInfo.getString("product_addr");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                if(info_id==null) continue;
//                                try {
//                                    dao.executeCommand(ApiConfig.SAAS_DATA, "insert into reptile_product_lhs_"+sysdate+" (biz,info_id,link,info_title,img,area_id,area_name,tree_name,order_start_date,order_end_date,price,market_price,order_num,address,storage,context, out_area_id, out_area_name)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
//                                            new Object[]{biz, info_id, link, info_title, img, area_id,area_name,tree_name, order_start_date, order_end_date, price, market_price, order_num, address, storage, context, out_area_id, out_area_name});
//                                    sum++;
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }else{
//                        //表示此编号不存在城市
//                        continue;
//                    }
//                } catch (Exception e) {
//                    PeptileTaskLog.pullproductLog("旅划算抢购","采集失败"+e.getMessage(),0,state);
//                }
//            }
//            if(sum>0){
//                state = "1";
//                logger.info("旅划算抢购产品采集完成！一共采集了"+sum+"件产品！");
//                PeptileTaskLog.pullproductLog("旅划算抢购","旅划算抢购采集成功！一共爬取了"+sum+"件产品",sum,state);
//            }else{
//                PeptileTaskLog.pullproductLog("旅划算抢购","采集失败",0,state);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//
////   @Scheduled(cron = "0 0 1 * * ?")
////    @SchedulerLock(name = "lhuasuanJob", lockAtLeastFor = 1000 * 30)
//    public void lhuasuanJob(){
//        int sum = 0;
//        String state = "0";
//        Map<String,String> map = new HashMap<String, String>();
//        //获取当前系统日期
//        //String sysdate = UWDate.getToday().replace("-","");
//        //获取当前系统日期
//        LocalDate date = LocalDate.now();
//        String sysdate = date.format(DateTimeFormatter.BASIC_ISO_DATE);
//        try{
//            logger.info("#####################开始定时采集旅划算#####################");
//            //根据字典查询出所有城市
//            DataSet areas = dao.queryForDataSet(ApiConfig.SAAS_DATA,"select lhs_area_id,lhs_area_name from reptile_product_area_lhs");
//            while (areas.next()){
//                try {
//                    String params="";
//                    map.put("currentCityId",areas.getString("lhs_area_id"));
//                    map.put("&simplePageParam","50%2C2");
//                    map.put("&distributorLevel","-1");
//                    map.put("&rebateUp","-1");
//                    map.put("&superRebateDown","-1");
//                    for (String key : map.keySet()) {
//                        params+=key + "=" + map.get(key);
//                    }
//                    String jsonStr= HttpUtil.doGet("https://fx3-guangdong.lhs11.com/fxapi/services/v2/product@cityProductList.japi?",params);
//                    //字符串转为Json
//                    JSONObject jsonObject=JSONObject.fromObject(jsonStr);
//                    //得到第一个data对象
//                    JSONObject result = null;
//                    try {
//                        result = jsonObject.getJSONObject("result");
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    if(result!=null){
//                        //得到productList对象集
//                        JSONArray productList = null;
//                        try {
//                            productList = result.getJSONArray("productList");
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        String biz="旅划算";
//
//                        String area_id = null;
//                        String out_area_name=null;
//                        //查询出编号所在的城市
//                        try {
//                            DataSet area = dao.queryForDataSet(ApiConfig.SAAS_DATA,"select *from reptile_product_area_lhs where lhs_area_id="+areas.getString("lhs_area_id"));
//                            while (area.next()){
//                                out_area_name = area.getString("lhs_area_name");
//                                area_id = AreaHelper.getAreaIDForName(area.getString("lhs_area_name"));
//                            }
//                        }catch (Exception e){
//                            e.getMessage();
//                        }
//                        String tree_name=null;
//                        String market_price = "0";
//                        String storage=null;
//                        String context=null;
//                        for (int j = 0; j <= productList.size()-1; j++) {
//                            try {
//                                String info_id = null;
//                                try {
//                                    info_id = productList.getJSONObject(j).optString("id");
////                                    if(dao.queryForDataSet(ApiConfig.SAAS_DATA,"select info_id from reptile_product where biz='lhs' and info_id="+info_id).size()>0){
////                                        dao.executeCommand(ApiConfig.SAAS_DATA,"insert into reptile_product_lhs_"+sysdate+" (biz,info_id)values(?,?)",
////                                                new Object[]{biz,info_id});
////                                        continue;
////                                    }
//                                } catch (Exception e) {
//
//                                }
//                                String info_title = null;
//                                try {
//                                    info_title = productList.getJSONObject(j).optString("name");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String img = null;
//                                try {
//                                    img = productList.getJSONObject(j).optString("img_main");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String order_start_date = null;
//                                try {
//                                    order_start_date = productList.getJSONObject(j).optString("begin_time");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//
//                                String order_end_date = null;
//                                try {
//                                    order_end_date = productList.getJSONObject(j).optString("end_time");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//
//                                String out_area_id = null;
//                                try {
//                                    out_area_id = productList.getJSONObject(j).optString("city");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String price = "0";
//                                try {
//                                    price = productList.getJSONObject(j).optString("price");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String order_num = null;
//                                try {
//                                    order_num = productList.getJSONObject(j).optString("saled_num");
//                                } catch (Exception e) {
//                                    PeptileTaskLog.pullproductLog("旅划算","采集失败"+e.getMessage(),0,state);
//                                }
//                                String link="https://cdn-fx3-api.lhs11.com/fxc/services/ms/fxc/product@getProductDetail.japi?variables%5Bproduct_id%5D="+info_id+"&variables%5Bclient_width%5D=450";
//                                //下面请求详情获取详情中的内容
//                                JSONObject xiangqing = null;
//                                try {
//                                    xiangqing = JSONObject.fromObject(HttpUtil.doGet(link,""));
//                                } catch (Exception e) {
//                                    PeptileTaskLog.pullproductLog("旅划算","采集失败"+e.getMessage(),0,state);
//                                }
//                                JSONObject xiangqing_result = null;
//                                try {
//                                    xiangqing_result = xiangqing.getJSONObject("result");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                JSONObject xiangqing_productInfo = null;
//                                try {
//                                    xiangqing_productInfo = xiangqing_result.getJSONObject("productInfo");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                try {
//                                    //当前路径：result.productInfo.skuList[3].quantity
//                                    storage= xiangqing_productInfo.getJSONArray("skuList").getJSONObject(0).optString("quantity");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                String address = null;
//                                try {
//                                    address = xiangqing_productInfo.optString("product_addr");
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                if(info_id==null)
//                                    continue;
//                                try {
//                                    dao.executeCommand(ApiConfig.SAAS_DATA, "insert into reptile_product_lhs_"+sysdate+" (biz,info_id,link,info_title,img,area_id,area_name,tree_name,order_start_date,order_end_date,price,market_price,order_num,address,storage,context, out_area_id, out_area_name)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
//                                            new Object[]{biz, info_id, link, info_title, img, area_id, out_area_name, tree_name, order_start_date, order_end_date, price, market_price, order_num, address, storage, context, out_area_id, out_area_name});
//                                    sum++;
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                            } catch (Exception e) {
//                                PeptileTaskLog.pullproductLog("旅划算","采集失败"+e.getMessage(),0,state);
//                            }
//
//                        }
//                    }else{
//                        //表示此编号不存在城市
//                        continue;
//                    }
//                } catch (Exception e) {
//                    PeptileTaskLog.pullproductLog("旅划算","采集失败"+e.getMessage(),0,state);
//                }
//
//            }
//            if(sum>0){
//                logger.info("旅划算产品采集完成！一共采集了"+sum+"件产品！");
//                PeptileTaskLog.pullproductLog("旅划算","旅划算采集成功！一共爬取了"+sum+"件产品",sum,"1");
//            }else{
//                PeptileTaskLog.pullproductLog("旅划算","采集失败",0,state);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}
